package com.zego.wawaji.third.auth;

import android.os.Parcel;
import android.os.Parcelable;

import org.json.JSONObject;

/**
 * 三方用户数据
 */
public class ThirdInfo implements Parcelable{

    private String nickName; // 用户昵称
    private String avatar;  // 用户头像地址， qq:40*40
    private String avatarLarge; // 用户大头像地址 qq: 100*100
    private String avatarHd;  // 高清头像地址
    private int gender = 1;   // 性别 1：男 2：女  默认为男性
    private String openid;  // 登录后的openid
    private String unionid; // 微信登陆后的unionid 仅微信有效

    private String location; // 用户所在地
    private String country;  // 国（微信）
    private String province; // 省
    private String city;     // 市

    private int plat;  // 平台

    /**
     * qq用户信息
     */
    public static ThirdInfo qq(JSONObject json) {
        if (json != null && json.optInt("ret", -1) == 0) {
            ThirdInfo u = new ThirdInfo();
            u.nickName = json.optString("nickname");
            u.avatar = json.optString("figureurl_qq_1");
            u.avatarLarge = json.optString("figureurl_qq_2");
            u.province =  json.optString("province");
            u.city = json.optString("city");
            u.location = json.optString("province") + " " + json.optString("city");
            u.gender = "男".equals(json.optString("gender")) ? 1 : 2;

            return u;
        }
        return null;
    }

    /**
     * 微信用户信息
     */
    public static ThirdInfo wechat(JSONObject json) {
        if (json != null) {
            ThirdInfo u = new ThirdInfo();
            u.nickName = json.optString("nickname");
            u.openid = json.optString("openid");
            u.unionid = json.optString("unionid");
            u.avatar = json.optString("headimgurl");
            u.country = json.optString("country");
            u.province = json.optString("province");
            u.city = json.optString("city");
            u.location = json.optString("country") + " " + json.optString("province") + " " + json.optString("city");
            u.gender = json.optInt("sex");

            return u;
        }
        return null;
    }

    public String getNickName() {
        return nickName;
    }

    public String getLocation() {
        return location;
    }

    public String getCountry() {
        return country;
    }

    public String getProvince() {
        return province;
    }

    public String getCity() {
        return city;
    }

    public int getGender() {
        return gender;
    }

    public String getAvatar() {
        return avatar;
    }

    public String getAvatarLarge() {
        return avatarLarge;
    }

    public String getAvatarHd() {
        return avatarHd;
    }

    public String getOpenid() {
        return openid;
    }

    public String getUnionid() {
        return unionid;
    }

    public void setOpenid(String openid) {
        this.openid = openid;
    }

    public void setUnionid(String unionid) {
        this.unionid = unionid;
    }

    public int getPlat() {
        return plat;
    }

    public void setPlat(int plat) {
        this.plat = plat;
    }

    @Override
    public String toString() {
        return "ThirdInfo{" +
                "nickName='" + nickName + '\'' +
                ", location='" + location + '\'' +
                ", avatar='" + avatar + '\'' +
                ", avatarLarge='" + avatarLarge + '\'' +
                ", avatarHd='" + avatarHd + '\'' +
                ", gender=" + gender +
                ", openid='" + openid + '\'' +
                ", unionid='" + unionid + '\'' +
                '}';
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.nickName);
        dest.writeString(this.avatar);
        dest.writeString(this.avatarLarge);
        dest.writeString(this.avatarHd);
        dest.writeInt(this.gender);
        dest.writeString(this.openid);
        dest.writeString(this.unionid);
        dest.writeString(this.location);
        dest.writeString(this.country);
        dest.writeString(this.province);
        dest.writeString(this.city);
        dest.writeInt(this.plat);
    }

    public ThirdInfo() {
    }

    private ThirdInfo(Parcel in) {
        this.nickName = in.readString();
        this.avatar = in.readString();
        this.avatarLarge = in.readString();
        this.avatarHd = in.readString();
        this.gender = in.readInt();
        this.openid = in.readString();
        this.unionid = in.readString();
        this.location = in.readString();
        this.country = in.readString();
        this.province = in.readString();
        this.city = in.readString();
        this.plat = in.readInt();
    }

    public static final Creator<ThirdInfo> CREATOR = new Creator<ThirdInfo>() {
        @Override
        public ThirdInfo createFromParcel(Parcel source) {
            return new ThirdInfo(source);
        }

        @Override
        public ThirdInfo[] newArray(int size) {
            return new ThirdInfo[size];
        }
    };
}
